<template>
  <div>
    <el-upload
      action=""
      list-type="picture-card"
      :http-request="handleUpload"
      :show-file-list="false"
      :accept="accepts"
      class="upload-container"
      v-if="!fileList || fileList.length === 0"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <div class="file-list" v-loading="loading">
      <div class="item" v-for="(item, index) in fileList" :key="index">
        <viewer :ref="`viewerRef${index}`">
          <img style="width: 100px; height: 100px" :src="item.url" />
        </viewer>

        <div class="option">
          <div class="preview" @click="handlePreview(item.url, index)">
            <i class="el-icon-view" />
          </div>
          <div class="delete" @click="handleDelete(index, item)">
            <i class="el-icon-delete" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { uploadFile, deleteFile, previewFile } from "@/api/commons";
export default {
  name: "singleUpload",
  props: {
    //回显数据
    data: {
      type: Array,
      default: () => []
    },
    //接收类型，默认只接收图片
    accept: {
      trueType: String,
      default: "image/*"
    },
    //文件存储目录
    folder: {
      type: String,
      default: ""
    }
  },

  data() {
    return {
      fileList: this.data,
      accepts: this.accept,
      loading: false
    };
  },
  watch: {
    data: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.fileList = newVal;
        }
      },
      deep: true // 深度监听对象内部变化
    }
  },
  methods: {
    //上传图片
    async handleUpload(e) {
      this.loading = true;
      const file = e.file;
      const formData = new FormData();
      formData.append("file", file);
      formData.append("folder", this.folder);

      uploadFile(formData)
        .then(res => {
          this.loading = false;
          if (res.code !== 200) {
            return;
          }
          this.fileList = [
            {
              ...res.data,
              url: res.data.fileFolder + res.data.filePath,
              name: res.data.fileName
            }
          ];
          this.$emit("on-change", this.fileList);
        })
        .catch(err => {
          this.loading = false;
        });
      // const preview = await previewFile(res.data.id);
      // const url = URL.createObjectURL(preview.data);
    },

    //删除图片
    async handleDelete(key, item) {
      this.fileList.splice(key, 1);
      if (item.id) {
        await deleteFile(item.id);
      }
    },
    //预览图片
    handlePreview(url, index) {
      const key = `viewerRef${index}`;
      this.$refs[key][0].$viewer.show();
    }
  }
};
</script>
<style lang="scss">
.upload-container {
  .el-upload {
    width: 100px;
    height: 100px;
    line-height: 100px;
  }
}

.file-list {
  display: flex;
  flex-flow: row wrap;
  margin-top: 15px;
  position: relative;

  .el-loading-mask {
    width: 100px;
    height: 100px;
    top: -115px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 5px;
  }

  .item {
    width: 100px;
    margin: 0 15px 15px 0;
    text-align: center;
    position: relative;
    float: left;
    transition: all 3s;

    img {
      border: solid 1px #f1f1f1 !important;
      border-radius: 5px;
    }

    .name {
      font-size: 12px;
    }

    .option {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100px;
      background-color: rgba(0, 0, 0, 0.5);
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 5px;
      visibility: hidden;

      div {
        width: 30px;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      i {
        color: #fff;
        cursor: pointer;
        font-size: 18px;
      }
    }

    &:hover {
      .option {
        visibility: visible;
      }
    }
  }
}
</style>
